<?php

namespace App\Models;

use App\Service\BangCache;

class Admin extends BaseUser
{

    // 用户管理
    protected $table = 'admin';

    // 缓存字段，用于搜索绑定的字段
    protected $cacheFields = ['id'];

    // 获取所有的
    public function getAll($where = [], $order = 'id ASC')
    {
        // 字段
        $fields = $this->getTableField(true);
        $fields = array_diff($fields, ['role', 'password']);
        // 查询
        $lists = $this->where($where)->select($fields)->orderByRaw($order)->get()->toArray();
        $lists = array_column($lists, null, 'id');


        return $lists;
    }

    // 权限菜单
    public function getAllMenu()
    {
        $menu = [
            ['role' => 'dashboard', 'name' => '首页', 'en_name' => 'home page',],

            // 管理员管理
            ['role' => 'admin', 'name' => '管理员管理', 'en_name' => 'Administrator management',],
            ['role' => 'admin_role', 'name' => '角色管理', 'en_name' => 'Roles',],
            ['role' => 'admin_log', 'name' => '日志管理', 'en_name' => 'Log management',],

            // 商品管理
            ['role' => 'goods', 'name' => '商品管理', 'en_name' => 'Commodity management',],
            ['role' => 'goods_cate', 'name' => '商品分类', 'en_name' => 'CATEGORY',],

            // 用户管理
            ['role' => 'user', 'name' => '用户管理', 'en_name' => 'User Management',],

            // 订单管理
            ['role' => 'goods_comment', 'name' => '商品评论', 'en_name' => 'Product Review',],
            ['role' => 'order', 'name' => '订单管理', 'en_name' => 'order management',],

            // 系统配置
            ['role' => 'site_config', 'name' => '系统配置', 'en_name' => 'system configuration',],
            ['role' => 'site_info', 'name' => '单页配置', 'en_name' => 'Single page configuration',],
        ];

        return $menu;
    }



    // 获取某个用户的权限菜单
    public function getUserMenu($uid, $roles)
    {
        // 如果是超级管理员
        $menu = $this->getAllMenu();
        if($uid == 1){
            return $menu;
        }

        // 匹配权限的菜单
        $menus = [];
        foreach($roles as $role){
            if(isset($menu[$role])){
                $menus = $role;
            }
        }

        return $menus;
    }

    // 检查权限
    public function checkRole($roles, $controller)
    {
        // 如果是公共操作

        // 基础：只有控制器的权限判断
        if(in_array($controller, $roles)){
            return true;
        }


        return false;
    }

    // 登录的字段
    protected function loginField()
    {
        // 'last_time', 'last_ip',
        return ['id', 'username', 'password', 'salt', 'status', 'role', 'mobile', 'nickname', ];
    }

    // 定义验证操作
    public function rules()
    {

        $add = [
            'username' => 'required',
            'salt'     => 'required',
        ];

        $edit = [
        ];

        $info = [
            'password' => ['max:129'],
            'is_del'   => ['in:0,1,2'],
            //'role'     => ['in:0,1,2'],
        ];

        $rule = [
            //'nickname' => 'required',
            //'role'       => ['required'],
            //'article_no' =>['numeric'],
            //'mobile'     =>['required'],
            'password' => ['max:65'],
            'role'     => ['in:0,1,2'],
            'status'   => ['in:0,1,2'],
        ];


        $msg = [
            'id'         => '编号',
            'nickname'   => '姓名',
            'username'   => '用户名',
            'mobile'     => '手机号',
            'password'   => '密码',
            'role'       => '角色',
            'article_no' => '创建文章数',
            'salt'       => '随机码',
            'status'     => '状态',
            'ctime'      => '创建时间',
            'mtime'      => '修改时间',
        ];

        return ['rule' => $rule, 'msg' => $msg, 'add' => $add, 'info' => $info];
    }

}
